/*
 * Created on Jan 22, 2011 by Eng. Julian Rousselot
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package ar.com.visualAORE.util;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.swingx.VerticalLayout;

import ar.com.visualAORE.model.Document;
import ar.com.visualAORE.model.Selection;
import ar.com.visualAORE.view.ReqInternalFrame;

import com.nitido.utils.toaster.Toaster;

/**
 * @author julian <br>
 *        Esta clase crea el grupo de botones que corresponderan a la parte
 *         grafica de un documento
 */
public class ButtonsGraphics extends JPanel {

	private static final long serialVersionUID = 1L;
	Document document;
	/* the default image to use */
	String imageFile = "/icons/document.jpg";
	ReqInternalFrame rif;

	public ButtonsGraphics(Document document, ReqInternalFrame rif, int zoom, int threshold) {
		super();
		this.document = document;
		initComponents(zoom, threshold);
		ToasterMessage.showMessage("This is a graphic view of the document");
		this.rif = rif;
		//this.setLayout(mgr);
	}

	public void paintComponent(Graphics g) {
		// create image icon to get image
		ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
		Image image = imageicon.getImage();

		// Draw image on the panel
		super.paintComponent(g);

		if (image != null)
			g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
	}

	public void initComponents(int zoom, int threshold) {
		this.setLayout(new VerticalLayout(FlowLayout.CENTER));
		Selection selection = null;
		Collections.sort(this.document.getSelections());
		JLabel labelDoc = new JLabel("");
		labelDoc.setPreferredSize(new Dimension(30, 50));
		this.add(labelDoc);

		Iterator<Selection> it = this.document.getSelections().iterator();
		while (it.hasNext()) {
			selection = it.next();
			
			//JPanel panelButonCheckBox = new JPanel();
			//panelButonCheckBox.setOpaque(false);
			
			final JButton btn = new JButton();
			btn.setBackground(selection.getConcern().getColor());
			// Es el porcentaje que representa la seleccion comparada con el
			// total del cocumento
			int porcent = (int) getSelectionPorcent(selection.getSize());
			// Si el porcentaje es mayor o igual que el threshold se muestra,
			// sino no
			System.out.println("PORCENT: " + porcent);
			if (porcent >= threshold) {
				if(porcent < 1)
					porcent = 2;
				btn.setPreferredSize(new Dimension(250, porcent * zoom));
				btn.setToolTipText(selection.getConcern().getName()
						+ " "
						+ String.format("%.3g%n", getSelectionPorcent(selection
								.getSize())) + "%");
				// Para que cuando se presiona el boton nos lleve a la seccion
				// del documento al que refiere
				final int position = selection.getDot();
				btn.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						rif.goToSpecificPosition(position);
					//	rif.getJtextPane().setCaretPosition(position);
						rif.getRadioButtonText().setSelected(true);
						rif.textControls(true);
						rif.mapControls(false);	

					}
				});

				this.add(btn);
				//panelButonCheckBox.add(btn);
				final JCheckBox showConcernChckBox = new JCheckBox("Show");
				showConcernChckBox.setOpaque(false);
				showConcernChckBox.setSelected(true);
				showConcernChckBox.setToolTipText(selection.getConcern().getName()
						+ " "
						+ String.format("%.3g%n", getSelectionPorcent(selection
								.getSize())) + "%");
				showConcernChckBox.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						if(showConcernChckBox.isSelected())
							btn.setVisible(true);
						else
							btn.setVisible(false);
						
					}
				});
			//	panelButonCheckBox.add(showConcernChckBox);
			//	this.add(panelButonCheckBox);
			}

		}
	}

	/**
	 * Devuelve el porcentaje con respecto al total del documento, de la
	 * selección que se pasa como parámetro
	 * 
	 * @param size
	 *            tamaño de la selección
	 * @return el porcentaje de la selección
	 */
	public float getSelectionPorcent(int size) {
		float result = (float) (size * 100)
				/ (float) this.document.getDocumentSize();
		return result;
	}
}
